package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.AdvertiserDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.AdvertiserDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * advertiser in this implementation.
 * 
 * @author Palaniappan Vairavan
 * 
 */
public class AdvertiserPropertyDAO extends PropertyDAO<AdvertiserDTO> implements
		AdvertiserDAO {
	/**
	 * Constructor that calls the super class PropertyDAO to create Advertiser
	 * Property DAO
	 * 
	 * @param jobAdvertProperties
	 */
	public AdvertiserPropertyDAO(String jobAdvertProperties) {
		super(jobAdvertProperties);
	}

	/**
	 * Convert DTO into properties given the Account DTO object
	 * 
	 * @return true if create Account DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(AdvertiserDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * Deletes an Advertiser object given its ID
	 * 
	 * @param Id
	 *            of the Advertiser object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		AdvertiserDTO advertiserDTO = retrieveById(id);
		advertiserDTO.setDeleted(true);
		convertDTOIntoProperties(advertiserDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all Advertiser objects in the persistence storage.
	 * 
	 * @return list of Advertiser DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<AdvertiserDTO> retrieveAll() throws DataAccessException {
		List<AdvertiserDTO> advertiserDTOList = new ArrayList<AdvertiserDTO>();
		int lastSequence = getLastSequence();
		AdvertiserDTO advertiserDTO = null;
		System.out.println("Properties in retrieve all" + getProperties());
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			advertiserDTO = new AdvertiserDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence),
					advertiserDTO);
			System.out.println("Advertiser DTO : " + advertiserDTO);
			advertiserDTOList.add(advertiserDTO);

		}
		return advertiserDTOList;
	}

	/**
	 * DAO will retrieve Advertiser objects in the persistence storage based on
	 * the id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Advertiser DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public AdvertiserDTO retrieveById(Object id) throws DataAccessException {

		AdvertiserDTO advertiserDTO = new AdvertiserDTO();
		populateValuesIntoDTO((String) id, advertiserDTO);
		return advertiserDTO;
	}

	/**
	 * DAO will update Advertiser objects in the persistence storage based
	 * 
	 * @param AccountDTO
	 *            Advertiser DataTransfer Object
	 * @return list of Advertiser DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public AdvertiserDTO update(AdvertiserDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

	/**
	 * DAO will retrieve Advertiser objects in the persistence storage based on
	 * the id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Advertiser DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public AdvertiserDTO retrieveByAccountId(String accountId)
			throws DataAccessException {

		List<AdvertiserDTO> allAdvertisers = retrieveAll();

		if (allAdvertisers != null && allAdvertisers.size() > 0) {
			for (AdvertiserDTO advertiserDTO : allAdvertisers) {
				if (advertiserDTO.getAccountId().equalsIgnoreCase(accountId)) {
					return advertiserDTO;
				}
			}

		}
		return null;
	}

}
